<?php

class Anderles_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract {

	public function routeShutdown(Zend_Controller_Request_Abstract $request) {

		$locale = new Zend_Locale();

		$options = array(
			'scan' => Zend_Translate::LOCALE_DIRECTORY,
			'disableNotices' => true
		);
		$translate = new Zend_Translate(
				'gettext',
				APPLICATION_PATH . '/lang', 'auto', $options);

		$requestParams = $this->getRequest()->getParams();
		$language = (isset($requestParams['language'])) ? $requestParams['language'] : false;
		if ($language == false) {
			$language = ($translate->isAvailable($locale->getLanguage())) ? $locale->getLanguage() : 'ru';
		}
		if (!$translate->isAvailable($language)) {
			throw new Zend_Controller_Action_Exception('This page dont exist', 404);
		} else {
			$locale->setLocale($language);
			$translate->setLocale($locale);

			Zend_Form::setDefaultTranslator($translate);


//			setcookie('lang', $locale->getLanguage(), null, '/');

			Zend_Registry::set('Zend_Locale', $locale);
			Zend_Registry::set('Zend_Translate', $translate);
		}
	}

}